package com.wx.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.wx.entity.SysRole;
import com.wx.entity.SysRoleExample;
import com.wx.entity.SysUserRole;
import com.wx.entity.SysUserRoleExample;
import com.wx.exception.BussException;
import com.wx.service.SysRoleService;
import com.wx.service.SysUserRoleService;
import com.wx.service.SysUserSecurityService;
import com.wx.service.SysWxuserService;
import com.zyht.bank.entity.AxdOrgInfo;
import com.zyht.bank.service.AxdOrgInfoService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.util.Page;
import com.zyht.website.entity.SysUser;
import com.zyht.website.entity.SysUserExample;
import com.zyht.website.service.SysUserService;
import com.zyht.website.vo.SysUserVO;

/**
 * 
 * 用户管理Controller
 */
@Controller
@RequestMapping("admin/user")
public class UserManageController extends AbsManageBaseController {

	@Resource
	private SysUserRoleService sysUserRoleService;
	@Resource
	private SysRoleService sysRoleService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	private SysWxuserService sysWxuserService;
	@Resource
	private SysUserSecurityService userSecurityService;
	@Resource
	private AxdOrgInfoService axdOrgInfoService;

	private final String ROLE_STATUS_STOP = "0";

	private final String ROLE_STATUS_USING = "1";

	private final String ROLE_STATUS_SYSTEM = "99";

	private final String USER_STATUS_SYSTEM = "9";

	@RequestMapping("/manage")
	@RequiresPermissions(value = { "admin:user:manage" })
	public String index(Integer currentPage, String username, Model model) {

		SysUserExample example = new SysUserExample();
		com.zyht.website.entity.SysUserExample.Criteria criteria = example.createCriteria();
		criteria.andStatusNotEqualTo(USER_STATUS_SYSTEM);
		if (!StringUtils.isEmpty(username)) {
			criteria.andUsernameLike("%" + username + "%");
		}
		int total = sysUserService.countByExample(example);
		String pageSizeStr = "20";
		int pageSize = Integer.parseInt(pageSizeStr);
		example.setOrderByClause("createtime desc");
		if (null == currentPage || 0 == currentPage) {
			currentPage = 1;
		}
		PageHelper.startPage(currentPage, pageSize);
		List<SysUser> resList = sysUserService.selectByExample(example);
		/*
		 * for (SysUser sysUser : resList) { try { //查询用户是否绑定过微信 List<SysWxuser>
		 * sysWxUserList = sysWxuserService.selectByUid(sysUser.getId()); String
		 * createtime = sysUser.getCreatetime(); String lasttime =
		 * sysUser.getLasttime(); if(!StringUtils.isEmpty(createtime)){ String
		 * date = DateUtils.formatMillis(Long.parseLong(createtime));
		 * sysUser.setCreatetime(date); } if(!StringUtils.isEmpty(lasttime)){
		 * String lastdate = DateUtils.formatMillis(Long.parseLong(lasttime));
		 * sysUser.setLasttime(lastdate); }
		 * sysUser.setConnectnum(sysWxUserList==null ? 0 :
		 * sysWxUserList.size()); }catch (Exception e) {
		 * LogUtils.log(e.getMessage()); } }
		 */
		Page<SysUser> page = new Page<SysUser>(total, currentPage, pageSize);
		page.setRecordList(resList);
		model.addAttribute("page", page);
		model.addAttribute("username", username);
		return "admin/user/manage";
	}

	@RequestMapping("/editview")
	@RequiresPermissions(value = { "admin:user:editview" })
	public String addview(@RequestParam(value = "id", required = false) Integer id, Model model) {
		SysRoleExample example = new SysRoleExample();
		example.createCriteria().andRoleStatusEqualTo(ROLE_STATUS_USING);
		List<SysRole> roles = sysRoleService.list(example);
		model.addAttribute("roles", roles);

		SysUser sub = null;
		if (null != id) {
			sub = sysUserService.selectByPrimaryKey(id);
			if (sub == null) {
				return message(model, "参数错误", "admin/user/editview");
			}
			SysUserRoleExample exampler = new SysUserRoleExample();
			exampler.createCriteria().andUserIdEqualTo(id);
			List<SysUserRole> list = sysUserRoleService.list(exampler);
			if (list == null || list.isEmpty()) {
			} else {
				model.addAttribute("userrole", list.get(0));
			}
		} else {
			sub = new SysUser();
		}

		// 获取机构
		List<AxdOrgInfo> list = axdOrgInfoService.list(null);
		Integer orgId = sub.getOrgId();
		if (null != orgId) {
			AxdOrgInfo axdOrgInfo = axdOrgInfoService.findOneById(orgId);
			model.addAttribute("axdOrgInfo", axdOrgInfo);
		}
		model.addAttribute("bean", sub);
		model.addAttribute("orgInfos", list);

		return "admin/user/editview";
	}

	@RequestMapping("/save")
	@RequiresPermissions(value = { "admin:user:save" })
	public String save(SysUserVO bean, Model model) {
		if (bean.getUsername() == null || bean.getUsername().isEmpty()) {
			return message(model, "员工编号不能为空", "admin/user/editview");
		}
		if (bean.getRoleId() == null) {
			return message(model, "角色信息不能为空", "admin/user/editview");
		}
		if (bean.getStatus() == null) {
			return message(model, "用户状态不能为空", "admin/user/editview");
		}

		if (null != bean.getId()) {
			sysUserService.updateSysUser(bean.getUsername(), bean.getRoleId(), bean.getStatus(), bean.getId(),
					bean.getOrgId());
		} else {
			try {
				sysUserService.addSysUser(bean.getUsername(), bean.getRoleId(), bean.getStatus(), bean.getOrgId());
			} catch (BussException e) {
				return message(model, e.getMessage(), "admin/user/editview");
			}
		}
		return message(model, "操作成功", "admin/user/manage");
	}

	@RequestMapping("/del")
	@RequiresPermissions(value = { "admin:user:del" })
	public String del(@RequestParam(value = "id", required = false) Integer id, Model model) {
		if (id != null) {
		}
		return message(model, "操作成功", "admin/user/manage");
	}

	/**
	 * 重置用户
	 * 
	 * @author:InkYi
	 * @time:2016年11月2日 - 下午1:56:33
	 * @description:
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/reset")
	@RequiresPermissions(value = { "admin:user:reset" })
	public String reset(@RequestParam(value = "id", required = false) Integer id, Model model) {
		if (id != null) {
		}
		return message(model, "操作成功", "admin/user/manage");
	}

	private String message(Model model, String message, String redirect) {
		model.addAttribute("message", message);
		model.addAttribute("msg", message);
		model.addAttribute("redirect", redirect);
		return "message";
	}

}
